package rtp.model.dao;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

import rtp.model.Tarefa;

public class TarefaDAO implements ITarefaDAO {

	private String DataPath;

	public TarefaDAO(String datapath) {
		this.DataPath = datapath;
	}

	public void criarTarefa(Tarefa task) {
		try {

			File f = new File(this.DataPath);
			SAXBuilder sb = new SAXBuilder();
			Document doc;

			doc = sb.build(f);

			Element root = doc.getRootElement();
			Element tarefa = new Element("tarefa");

			root.addContent(tarefa);

			Element id = new Element("id");
			Element titulo = new Element("titulo");
			Element prazo = new Element("prazo");
			Element detalhes = new Element("detalhes");
			Element responsavel = new Element("responsavel");
			Element status = new Element("status");
			Element conclusao = new Element("conclusao");
			Element solucao = new Element("solucao");

			id.setText(task.getId());
			titulo.setText(task.getTitulo());
			prazo.setText(task.getPrazo());
			detalhes.setText(task.getDetalhes());
			responsavel.setText(task.getResponsavel());
			status.setText(task.getStatus());
			conclusao.setText(task.getDtConclusao());
			solucao.setText(task.getSolucao());

			tarefa.addContent(id);
			tarefa.addContent(titulo);
			tarefa.addContent(prazo);
			tarefa.addContent(detalhes);
			tarefa.addContent(responsavel);
			tarefa.addContent(status);
			tarefa.addContent(conclusao);
			tarefa.addContent(solucao);

			XMLOutputter xmlOutputter = new XMLOutputter();
			xmlOutputter.output(doc, new FileWriter(this.DataPath));

		} catch (JDOMException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

	public void concluirTarefa(Tarefa task) {
		try {
			File f = new File(this.DataPath);
			SAXBuilder sb = new SAXBuilder();
			Document doc;

			doc = sb.build(f);

			Element root = doc.getRootElement();

			List elements = root.getChildren();
			Iterator i = elements.iterator();

			while (i.hasNext()) {
				Element element = (Element) i.next();

				if (element.getChildText("id").equals(task.getId())) {
					Element tarefa = element;

					Element status = tarefa.getChild("status");
					Element conclusao = tarefa.getChild("conclusao");
					Element solucao = tarefa.getChild("solucao");

					status.setText(task.getStatus());
					conclusao.setText(task.getDtConclusao());
					solucao.setText(task.getSolucao());

					break;
				}
			}

			XMLOutputter xmlOutputter = new XMLOutputter();
			xmlOutputter.output(doc, new FileWriter(this.DataPath));
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Tarefa> listarTarefas(String status) {
		
		ArrayList<Tarefa> listTarefas = null; 
		
		try {
			File f = new File(this.DataPath);
			SAXBuilder sb = new SAXBuilder();
			Document doc;

			doc = sb.build(f);

			Element usuarios = doc.getRootElement();
			List elements = usuarios.getChildren();
			Iterator i = elements.iterator();

			listTarefas = new ArrayList<Tarefa>();
			while (i.hasNext()) {
				Element element = (Element)i.next();
				if (element.getChildText("status").equals(status)) {
					Element tarefa = element;
					Tarefa task = new Tarefa();
					task.setId(tarefa.getChildText("id"));
					task.setTitulo(tarefa.getChildText("titulo"));
					task.setPrazo(tarefa.getChildText("prazo"));
					task.setDetalhes(tarefa.getChildText("detalhes"));
					task.setResponsavel(tarefa.getChildText("responsavel"));
					task.setStatus(tarefa.getChildText("status"));
					task.setDtConclusao(tarefa.getChildText("conclusao"));
					task.setSolucao(tarefa.getChildText("solucao"));

					listTarefas.add(task);
				}
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return listTarefas;
	}

	public Tarefa retornarTarefa(String id) {
		
		Tarefa task = null;
		
		try {
			File f = new File(this.DataPath);
			SAXBuilder sb = new SAXBuilder();
			Document doc;

			doc = sb.build(f);

			Element usuarios = doc.getRootElement();
			List elements = usuarios.getChildren();
			Iterator i = elements.iterator();

			while (i.hasNext()) {
				Element element = (Element) i.next();
				if (element.getChildText("id").equals(id)) {
					Element tarefa = element;
					task = new Tarefa();
					task.setId(tarefa.getChildText("id"));
					task.setTitulo(tarefa.getChildText("titulo"));
					task.setPrazo(tarefa.getChildText("prazo"));
					task.setDetalhes(tarefa.getChildText("detalhes"));
					task.setResponsavel(tarefa.getChildText("responsavel"));
					task.setStatus(tarefa.getChildText("status"));
					task.setDtConclusao(tarefa.getChildText("conclusao"));
					task.setSolucao(tarefa.getChildText("solucao"));

					break;
				}
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return task;
	}
}
